﻿using System;
using System.Collections.Generic;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;
using Karma.Framework.Core.Domain;

namespace Karma.Framework.Core.Modules.Reports.Definition
{
    [ActiveRecord("[ReportDefinition]", Mutable = false), JoinedBase]
    public class Report : DomainObject
    {
        [JoinedKey]
        public override Int64 Id { get; set; }

        [Property(NotNull = true)]
        public string Name { get; set; }

        [Property(NotNull = true)]
        public string Description { get; set; }

        [Property(ColumnType = "StringClob", SqlType = "text")]
        public string Query { get; set; }

        [Property(NotNull = true)]
        public QueryLanguage Languaje { get; set; }

        public Criteria PreConfiguredFilter
        {
            get
            {
                if (_preConfiguredFilter!= null && _preConfiguredFilter.Count > 0)
                {
                    return _preConfiguredFilter[0];
                }
                return null;
            }
            set
            {
                if (_preConfiguredFilter == null)
                {
                    _preConfiguredFilter = new List<Criteria>
                                               {
                                                   value
                                               };
                }
                else
                {
                    _preConfiguredFilter[0] = value;
                }
            }
        }

        [HasMany(typeof(Criteria), Cascade = ManyRelationCascadeEnum.All,
            Lazy = false, ColumnKey = "[Report]", Table = "[Criteria]")]
        private IList<Criteria> _preConfiguredFilter { get; set; }

        [HasMany(typeof(ContextParameter), Cascade = ManyRelationCascadeEnum.All,
            Lazy = false, ColumnKey = "[Report]", Table = "[ContextParameter]")]
        public IList<ContextParameter> ContextParameters { get; set; }

        [HasMany(typeof(Filter), Cascade = ManyRelationCascadeEnum.All,
            Lazy = false, ColumnKey = "[Report]", Table = "[Filter]")]
        public IList<Filter> Filters { get; set; }

    }
}